import { Context } from 'koa';
import { calculateY, calculateOff } from '../utils/calculator';

interface CalculationRequest {
    profitValue: number;
    profitType: 'fixed' | 'cost' | 'price';
    cost: number;
    coupon: number;
    off: number;
    y: number;
    weight: number;
    withdrawalFeeRate?: number;
    platformCommissionRate?: number;
    sfpFeeRate?: number;
    talentCommissionRate?: number;
    transactionFeeRate?: number;
    vatRate?: number;
    bonusCashbackRate?: number;
    logisticsCostPer10g?: number;
    exchangeRate?: number;
    buyerShippingFee?: number;
}

export const calculatePrice = async (ctx: Context) => {
    try {
        const requestBody = ctx.request.body as CalculationRequest;
        const {
            profitValue,
            profitType,
            cost,
            coupon,
            off,
            y,
            weight,
            withdrawalFeeRate = 0.01,
            platformCommissionRate = 0.054,
            sfpFeeRate = 0,
            talentCommissionRate = 0.01,
            transactionFeeRate = 0.0378,
            vatRate = 0.1,
            bonusCashbackRate = 0.0435,
            logisticsCostPer10g = 0.15,
            exchangeRate = 1.6412,
            buyerShippingFee = 10
        } = requestBody;

        let result;
        if (y === undefined || y === 0) {
            // Calculate Y if not provided
            result = calculateY(
                Number(profitValue),
                profitType,
                Number(cost),
                Number(coupon),
                Number(off),
                Number(weight),
                Number(withdrawalFeeRate),
                Number(platformCommissionRate),
                Number(sfpFeeRate),
                Number(talentCommissionRate),
                Number(transactionFeeRate),
                Number(vatRate),
                Number(bonusCashbackRate),
                Number(logisticsCostPer10g),
                Number(exchangeRate),
                Number(buyerShippingFee)
            );
        } else if (off === undefined || off === 0) {
            // Calculate Off if Y is provided
            result = calculateOff(
                Number(y),
                Number(profitValue),
                profitType,
                Number(cost),
                Number(coupon),
                Number(weight),
                Number(withdrawalFeeRate),
                Number(platformCommissionRate),
                Number(sfpFeeRate),
                Number(talentCommissionRate),
                Number(transactionFeeRate),
                Number(vatRate),
                Number(bonusCashbackRate),
                Number(logisticsCostPer10g),
                Number(exchangeRate),
                Number(buyerShippingFee)
            );
        } else {
            throw new Error('Either y or off must be provided, but not both');
        }

        ctx.body = {
            success: true,
            data: result
        };
    } catch (error) {
        console.error('Error calculating price:', error);
        ctx.status = 500;
        ctx.body = {
            success: false,
            error: error instanceof Error ? error.message : 'Failed to calculate price'
        };
    }
}; 